package com.mall.biz.item;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mall.client.item.MallItemService;
import com.mall.pojo.MallItem;
import com.mall.pojo.MallItemExample;
import com.mall.pojo.MallItemExample.Criteria;
import com.mall.query.MallItemQuery;

@Service
public class MallItemBizImpl {
	@Autowired
	private MallItemService mallItemService;

	public List<MallItem> getMallItemListByMallIdAndOnlykey(Long mallId, Integer onlyKey) {
		MallItemExample example = new MallItemExample();
		example.setOrderByClause("sale_no desc");
		Criteria criteria = example.createCriteria();
		criteria.andStockGreaterThan(0);
		criteria.andStatusEqualTo(0);
		criteria.andMallIdEqualTo(mallId);
		List<MallItem> mallItemList = this.mallItemService.getMallItemListByExample(example);
		return mallItemList;
	}
	
	/**
	 * 根据query对象查询商品列表
	 * @param mallItemQuery
	 * @return
	 */
	public List<MallItem> getMallItemByQuery(MallItemQuery mallItemQuery){
		MallItemExample mallItemExample = new MallItemExample();
		mallItemExample.setOrderByClause(mallItemQuery.getOrderBy());
		if(mallItemQuery.getPage() != null){
			if(mallItemQuery.getPage().getBegin() >= 1){
				mallItemExample.setStart(mallItemQuery.getPage().getBegin());
			}
			if(mallItemQuery.getPage().getLength() > 0){
				mallItemExample.setLimit(mallItemQuery.getPage().getLength());
			}
		}
		Criteria criteria = mallItemExample.createCriteria();
		if(mallItemQuery.getItemCategoryId() != null && mallItemQuery.getItemCategoryId() > 0){
			criteria.andItemCategoryIdEqualTo(mallItemQuery.getItemCategoryId());
		}
		if(mallItemQuery.getOnlyKey() != null){
			criteria.andCommOnlyKeyEqualTo(mallItemQuery.getOnlyKey());
		}
		if(mallItemQuery.getStatus() != null){
			criteria.andStatusEqualTo(mallItemQuery.getStatus());
		}else{//如果不传则显示除删除删除意外的商品
			criteria.andStatusGreaterThanOrEqualTo(-1);
		}
		if(mallItemQuery.getStock() != null ){
			criteria.andStockEqualTo(mallItemQuery.getStock());
		}
		if(mallItemQuery.getMallId() != null && mallItemQuery.getMallId() > 0){
			criteria.andMallIdEqualTo(mallItemQuery.getMallId());
		}
		return this.mallItemService.getMallItemListByExample(mallItemExample);
	}
}
